<?php

class Default_PostController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
		$this->_auth = Zend_Auth::getInstance();
		$this->view->hasIdentity = $this->_auth->hasIdentity();
    }

    public function indexAction()
    {
	
		// echo $this->_helper->alias('Work on ‘Beatbox’, the elementary project’s media player, is continuing apace.');
		// die();
		$post= new Application_Model_Post();
		$all_post = $post->fetchAllOrderDesc(array(
			'published=1'
		));
		
		$paginator = Zend_Paginator::factory($all_post);
		$paginator->setItemCountPerPage(10);        
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->data_page=$paginator;
        
	//	$this->view->data = $all_post;
		$this->view->headTitle('Index Post');
    }
	
	public function viewAction() 
	{
		$request = $this->getRequest();
		$id = $request->getParam('id');
		$post= new Application_Model_Post();
		
		if (is_numeric($id))
			$result_post = $post->getById($id);
		else if (is_string($id)) {
			$result_post = $post->getByAlias($id);
			$id = $result_post->id;
		}
		$viewNamespace = new Zend_Session_Namespace('View');
		
		if (!isset($viewNamespace->$id)) {
			$viewNamespace->$id = $result_post->views +1;
			
			$post->updatePost(array(
				'views' => $result_post->views +1,
				), $id);
		}
		$this->view->headTitle($result_post->title);
		$this->view->post = $result_post;
	
	}
	
}

